package com.framework.main.sys.role.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import com.framework.base.struts.BaseAction;
import com.framework.base.struts.Result;
import com.framework.base.util.ParamsUrlUtil;
import com.framework.base.util.Struts2Utils;
import com.framework.main.sys.role.service.RoleService;
import com.framework.main.sys.user.domain.UserVo;

/**
 * Title:角色（Action）
 * Description:用于管理角色数据请求
 * tabaleName(对应的表名): SYS_ROLE
 * Copyright: 2011
 * Company: framework
 * @author：www.51wuxia.com
 * @version 1.0
 * @Date May 30, 2011 5:02:09 PM
 */
@SuppressWarnings("serial")
public class RoleAction extends BaseAction{
	private RoleService roleService;
	
	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	
	public RoleAction() {
		getLogger().info("roleaction init");
	}
	
	/**
	 * 用于删除指定的数据
	 * 
	 * @return
	 * @throws Exception
	 */
	public String doDelete() throws Exception {
		this.setParamsUrl(ParamsUrlUtil.dealParamsUrl(Struts2Utils.getRequest()));
		String[] pks=Struts2Utils.getRequest().getParameterValues("fid");
		String resultName =this.getRoleService().doDelete(pks);
		return resultName;
	}
	
	/**
	 * 显示人员选择数据
	 * @param cfg
	 * @return
	 * @throws Exception
	 */
	public String toselectUser()throws Exception{
		String pk = Struts2Utils.getRequest().getParameter("fid");
		if(pk==null||"".equals(pk)){
			pk=(String)Struts2Utils.getRequest().getAttribute("pk");
		}
		List<UserVo> userRole=this.roleService.toselectUser("getUserRole", pk);
		List<UserVo> userKxRole=this.roleService.toselectUser("getKxUserRole", pk);
		Result result=this.getRoleService().toList(Struts2Utils.getParameterMap(getQueryMap(),this.getVo()));
		Struts2Utils.getRequest().setAttribute("userRole", userRole);
		Struts2Utils.getRequest().setAttribute("userKxRole", userKxRole);
		Struts2Utils.getRequest().setAttribute("pk", pk);
		return result.getForwardName();
	}
	
	/**
	 * 选择人员
	 * @param cfg
	 * @return
	 * @throws Exception
	 */
	public String doInsertUR() throws Exception {
		String userid = Struts2Utils.getRequest().getParameter("users");
		String pk = Struts2Utils.getRequest().getParameter("pk");
		this.getRoleService().doInsertUR(pk, userid);
		Struts2Utils.getRequest().setAttribute("pk",pk);
		return this.toselectUser();
	}
	
	/**
	 * 修改分配角色的权限
	 * @return
	 * @throws Exception
	 */
	public String toDistribute()throws Exception{
		String pk=Struts2Utils.getRequest().getParameter("fid");
		Result result=this.getRoleService().toDistribute(pk);
		Set<String> keySet = result.getResultMap().keySet();
		for (String key : keySet) {//对参数的转发
			Struts2Utils.getRequest().setAttribute(key, result.getResultMap().get(key));
		}
		return result.getForwardName();
	}
	
	/**
	 * 保存角色分配权限的操作
	 * @return
	 * @throws Exception
	 */
	public String doDistribute()throws Exception{
		String pk=Struts2Utils.getRequest().getParameter("fid");
		String[] value=Struts2Utils.getRequest().getParameterValues("moduAndOper");
		String[] value2=Struts2Utils.getRequest().getParameterValues("r-1");
		String scope = Struts2Utils.getRequest().getParameter("dataScopef");
		Result result=this.getRoleService().doDistribute(pk,value,value2,scope);
		Set<String> keySet = result.getResultMap().keySet();
		for (String key : keySet) {//对参数的转发
			Struts2Utils.getRequest().setAttribute(key, result.getResultMap().get(key));
		}
		return result.getForwardName();
	}
}
